package com.dayu.common.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * @USER: dayu
 * @EMAIL: dayu007@foxmail.com
 * @DATE: 2020/5/14 13:54
 * @DESCRIPTION: 编写一个自定义的校验器
 */
public class ListValueConstraintValidator implements ConstraintValidator<ListValue, Integer> {

    private Set<Integer> set=new HashSet<>();

    /**
     * 指定脚尖的值
     * @param constraintAnnotation
     */
    @Override
    public void initialize(ListValue constraintAnnotation) {
        int[] values = constraintAnnotation.values();
        for (int i = 0; i <values.length; i++) {
            set.add(values[i]);
        }
    }

    /**
     * 校验结果
     * @param value 需要校验的值
     * @param constraintValidatorContext
     * @return
     */
    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext constraintValidatorContext) {
        return set.contains(value);
    }
}
